/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package treccontextsug;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;

/**
 *
 * @author manzato
 */
public class ProfilesSet extends Parser {

    private ArrayList<Profile> profiles;

    public ProfilesSet(String profilesFile) {
        super(profilesFile);
        this.profiles = new ArrayList();
    }

    @Override
    public void readJSON() throws IOException, ParseException {
        jsonObject = (JSONObject) parser.parse(new FileReader(filename));
        Set<String> set = jsonObject.keySet();

        for (String s : set) {
            profiles.add(new Profile(s));
        }

        for (Profile p : profiles) {
            JSONArray s = (JSONArray) jsonObject.get(p.getId());
            Iterator i = s.iterator();
            while (i.hasNext()) {
                JSONObject j = (JSONObject) i.next();
                String attractionId = (String) j.get("attraction_id");
                String website = (String) j.get("website");
                String description = (String) j.get("description");
                p.setDescriptionRating(attractionId, description);
                p.setWebsiteRating(attractionId, website);
            }
        }
    }

    public ArrayList<Profile> getAllProfiles() {
        return profiles;
    }

    public void buildProfiles(ExamplesSet exampleReader) {
        for (Profile p : profiles) {
            //System.out.println("Profile " + p.getId());
            ArrayList<String> positivePairs = new ArrayList();
            ArrayList<String> negativePairs = new ArrayList();
            for (Example e : exampleReader.getAllExamples()) {
                float rating = Float.valueOf(p.getWebsiteRating(e.getAttractionId()));
                String[] keywords = e.getKeywords();
                ArrayList<String> baseList = new ArrayList();
                ArrayList<String> specificList = new ArrayList();
                for (String k : keywords) {
                    if (ExamplesSet.baseKeywords.contains(k)) {
                        baseList.add(k);
                    } else {
                        specificList.add(k);
                    }
                }
                if (rating >= 2) { //interested                    
                    for (String base : baseList) {
                        for (String specific : specificList) {
                            String query = base + " " + specific;
                            if (!positivePairs.contains(query)) {
                                positivePairs.add(query);
                            }
                        }
                    }
                } else if (rating == 1 || rating == 0) {
                    for (String base : baseList) {
                        for (String specific : specificList) {
                            String query = base + " " + specific;
                            if (!negativePairs.contains(query)) {
                                negativePairs.add(query);
                            }
                        }
                    }
                }
            }
            p.setNegativeQueries(negativePairs.toArray(new String[negativePairs.size()]));
            p.setPositiveQueries(positivePairs.toArray(new String[positivePairs.size()]));
        }
    }
}
